package com.ruibang.glass.job.utils;

import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
import com.ruibang.glass.job.entity.DefectDataSource;
import com.ruibang.glass.job.entity.TimeInterval;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

public class JdbcOperUtils {



    public static String getLatelyTenMinsTimes(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, (calendar.get(Calendar.MINUTE))/10*10);
        calendar.set(Calendar.SECOND,0);
        Date late = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(late);
    }

    public static String getTenMinsTimesBefore(String dateTime){
        try{
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = dateFormat.parse(dateTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MINUTE,-20);
            Date late = calendar.getTime();
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(late);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    public static boolean isThreeeTimes(String dateTime){
        try{
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = dateFormat.parse(dateTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            return hour % 3 == 0;
        }catch (Exception e){
            e.printStackTrace();
        }
            return false;
    }

    public static String getFourHoursBefore(String dateTime){
        try{
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = dateFormat.parse(dateTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR,-4);
            Date late = calendar.getTime();
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(late);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    //获取数据库时间
    public static String getDBDate(String date,boolean decrement){
        if(decrement){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime localDateTime = LocalDateTime.parse(date, formatter).minusDays(1);
            formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            return localDateTime.format(formatter);
        }else{
            return date.substring(0, 10).replaceAll("-","");
        }
    }


    //连接数据库操作
    public static Connection getConnection(DefectDataSource defectDataSource, TimeInterval timeInterval,boolean decrement) throws Exception {

        String url = MessageFormat.format(defectDataSource.getUrl(),getDBDate(timeInterval.getEndTime(),decrement));
        String user = defectDataSource.getUsername();
        String password = defectDataSource.getPassword();
        String driverClass = defectDataSource.getDriverClass();
        // 2.加载驱动
        Class.forName(driverClass);
        // 3.获取连接
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(url, user, password);
        }catch (MySQLSyntaxErrorException e){
            System.out.println("no database found.");
            getConnection(defectDataSource, timeInterval,true);
        }
        return conn;
    }

    //资源的关闭操作
    public static void closeResource(Connection conn, Statement ps){
        try {
            if(ps != null)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
